/*
 * @Author: Oscar Pena
 * CS431 2011
 * 
 * */
package FlowGram.Application.SimulatorWindow.TerrainEditor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Iterator;


public class TerrainImage  extends javax.swing.JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static TerrainImage instance;
	
	private int Scale = 50;
	private Terrain terr;
	
	public TerrainImage(Terrain map){
		terr = map;
		terr.getFinch();
}
	public TerrainImage(){
		terr = null;
	}
	public static TerrainImage getInstance(){
		if(instance == null){
			//System.out.println("creating new TerrainImage()");
			instance = new TerrainImage();
		}
		return instance;
	}
	
	public void paintComponent(Graphics g){
		
		Graphics2D g2d = (Graphics2D)g;
		super.paintComponent(g2d);

		Dimension sizeimage = new Dimension();
		int xx = terr.getSizeX()*Scale;
		int yy = (terr.getSizeY()*Scale);
		int MapObjectNum = terr.getmapObjects().size();
		
		sizeimage.height = yy;
		sizeimage.width = xx;
		
		setBackground(Color.WHITE);
		g2d.setColor(Color.LIGHT_GRAY);
				
		sizeimage.height = yy+20;
		sizeimage.width = xx+20;
		setPreferredSize(sizeimage);
		
		for (int x = 0; x<=xx;x+=Scale){
			g2d.fillRect(x, 0, 3, yy+2);	
		}
		for (int y = 0; y<=yy;y+=Scale){
			
			g2d.fillRect(0, y, xx+2, 3);
		
		}
		
		//Draw objects in terrain
		Finch f = null;
		if (terr != null){
			
			Iterator<MapObject> itr = terr.getmapObjects().iterator();
			
			for(int i=0; i<MapObjectNum; i++){
				MapObject element = itr.next();
				int x = element.getPositionX();
				int y = element.getPositionY();
				if((x>=0 && y>=0) && ( x<terr.getSizeX() && y<terr.getSizeY())){
					
					if (element instanceof Wall) { 
						((Wall) element).Draw(g2d, Scale);
					}
					if (element instanceof Finch) {
						//finch is saved in to be drawn after all the objects
						//so it appears on top
						f = (Finch)element;
						//((Finch) element).Draw(g2d, Scale);
						}
					if (element instanceof Beeper) { 
						((Beeper) element).Draw(g2d, Scale);
						}
				}
			}
		}
		//before leaving the Finch is drawn so it appears in top if there is any other object in the tile
		f.Draw(g2d, Scale);
		
	}

	public Dimension getSize(){
	return super.getSize();
	}
	public void setScale(int inout){
		//@Description if inout == 1 increase scale by 10% if inout == 0 decreases scale by 10%
		//@Description scale has a maximum of 400 pixels and a minimum of 20
		int tempScale = Scale;
		if (inout == 1){
			tempScale = (int) (tempScale + (tempScale * 0.10));
			if (tempScale > 400) { tempScale = 400;}
			Scale = tempScale;
		} else if (inout == 0) {
			tempScale = (int) (tempScale - (tempScale * 0.10));
			if (tempScale < 20) { tempScale = 20; } 
			Scale = tempScale;
		}
		
	}
	public void set_map(Terrain map) {
		this.terr = map;
	}
	public int getScale(){
		return Scale;
	}
}
